package net.hanjava.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Date;
import java.util.Vector;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;

public class OptimizaedDrawingSample extends JPanel {
	private JPanel glass = null;

	public OptimizaedDrawingSample(JComponent child) {
		glass = new JPanel(new BorderLayout()) {
			@Override
			protected void paintComponent(Graphics g) {
				g.setColor(new Color(0, 255, 0, 128));
				g.fillRect(0, 0, getWidth(), getHeight());
			}
		};
		glass.setOpaque(false);
		add(glass);
		add(BorderLayout.CENTER, child);
	}

	@Override
	public void doLayout() {
		super.doLayout();
		// cover whole component
		glass.setBounds(0, 0, getWidth(), getHeight());
	}

	@Override
	public boolean isOptimizedDrawingEnabled() {
		return false;
	}

	private static JList createSampleList() {
		Vector data = new Vector();
		for(int i=0;i<20;i++) {
			data.add(new Date());
		}
		JList result = new JList(data);
		return result;
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame("Optimized Drawing Error Sample");
		
		frame.add(new OptimizaedDrawingSample( createSampleList() ));
		frame.pack();
		frame.setVisible(true);
	}
}